package com.anytrek.demowebclient.api;

import com.anytrek.demowebclient.entity.UserEntity;
import com.anytrek.demowebclient.utils.ApiResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;

@Log4j2
@Component
public class ApiService {

    @Autowired
    WebClient.Builder webClientBuilder;

    //get拼接参数
    public Mono<ApiResult> getParamApi(Integer id) {
        return webClientBuilder.baseUrl("http://localhost:8081")
                .build()
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path("/api")
                        .queryParam("id", id)
                        .build())
                .header("token", "token")
                .retrieve()
                .bodyToMono(ApiResult.class)
                .doOnSuccess(result -> log.info("请求成功: 方法=GET, URL=/api, 响应={}", result))
                .doOnError(error -> log.error("getParamApi 请求失败: {}", error.getMessage()));
    }

    //post拼接参数
    public Mono<ApiResult> postParamApi(String name) {
        return webClientBuilder.baseUrl("http://localhost:8081")
                .build()
                .post()
                .uri(uriBuilder -> uriBuilder
                        .path("/api")
                        .queryParam("name", name)
                        .build())
                .header("token", "token")
                .retrieve()
                .bodyToMono(ApiResult.class)
                .doOnSuccess(result -> log.info("请求成功: 方法=POST, URL=/api, 响应={}", result))
                .doOnError(error -> log.error("postParamApi 请求失败: {}", error.getMessage()));
    }

    //get传Restful
    public Mono<ApiResult> getRestFulApi(Integer id) {
        return webClientBuilder.baseUrl("http://localhost:8081")
                .build()
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path("/api/restful/{id}")
                        .build(id))
                .header("token", "token")
                .retrieve()
                .bodyToMono(ApiResult.class)
                .doOnSuccess(result -> log.info("请求成功: 方法=GET, URL=/api/restful/{id}, 响应={}", result))
                .doOnError(error -> log.error("getRestFulApi 请求失败: {}", error.getMessage()));
    }

    //Post传body
    public Mono<ApiResult> postBodyApi(UserEntity userEntity) {
        return webClientBuilder
                .baseUrl("http://localhost:8081")
                .build()
                .post()
                .uri("/api/update")
                .header("token", "token")
                .contentType(MediaType.APPLICATION_JSON)
                .bodyValue(userEntity)
                .retrieve()
                .bodyToMono(ApiResult.class)
                .doOnSuccess(result -> log.info("请求成功: 方法=POST, URL=/api/update, 响应={}", result))
                .doOnError(error -> log.error("postBodyApi 请求失败: {}", error.getMessage()));
    }
}